package com.aaa.sts.service.impl;

import com.aaa.common.entity.StoreLabels;
import com.aaa.common.util.TokenUtil;
import com.aaa.common.vo.StoreLabelsVO;
import com.aaa.sts.dao.StoreLabelsDao;
import com.aaa.sts.service.RemoteRoleService;
import com.aaa.sts.service.StoreLabelsService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.aaa.common.util.CustomException;
import com.aaa.common.util.ResultStatus;
import com.aaa.common.vo.Page;


import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.util.List;

/**
 * (StoreLabels)表服务实现类
 *
 * @author makejava
 * @since 2025-09-18 14:54:44
 */
@Service("storeLabelsService")
public class StoreLabelsServiceImpl implements StoreLabelsService {
    @Resource
    private StoreLabelsDao storeLabelsDao;
    @Resource
    private RemoteRoleService RemoteRoleService;
    @Resource
    private HttpServletRequest request;
    @Autowired
    private RemoteRoleService remoteRoleService;

    /**
     * 通过ID查询单条数据
     *
     * @param labelId 主键
     * @return 实例对象
     */
    @Override
    public StoreLabelsVO queryById(Integer labelId) {
        return this.storeLabelsDao.queryById(labelId);
    }

    /**
     * 分页查询
     *
     * @param page 筛选条件
     * @return 查询结果
     */
    @Override
    public PageInfo queryByPage(Page<StoreLabelsVO> page) {
        Integer userId = Integer.parseInt(TokenUtil.getUserId(request));
        //分页操作  分页参数一定要进行传递
        if (page == null || page.getPageNo() == null || page.getPageNo() == 0 || page.getPageSize() == null || page.getPageSize() == 0) {
            //进行业务编写时，可以抛出自定义异常
            throw new CustomException(ResultStatus.PARAM_NOT_EMPTY.getReturnCode(),
                    ResultStatus.PARAM_NOT_EMPTY.getReturnMessage());
        }
        PageHelper.startPage(page.getPageNo(), page.getPageSize());
        //获取查询对象
        StoreLabelsVO storeLabels = page.getData();
        List<String> roleKeyList = remoteRoleService.queryRoleKey(userId);
        if (roleKeyList.contains("laoban")){
            storeLabels.setUserId(userId);
        }
        return new PageInfo(storeLabelsDao.queryAllByLimit(storeLabels));
    }

    /**
     * 新增数据
     *
     * @param storeLabels 实例对象
     * @return 实例对象
     */
    @Override
    public StoreLabels insert(StoreLabels storeLabels) {
        this.storeLabelsDao.insert(storeLabels);
        return storeLabels;
    }

    /**
     * 修改数据
     *
     * @param storeLabels 实例对象
     * @return 实例对象
     */
    @Override
    public StoreLabelsVO update(StoreLabelsVO storeLabels) {
        this.storeLabelsDao.update(storeLabels);
        return this.queryById(storeLabels.getLabelId());
    }

    /**
     * 通过主键删除数据
     *
     * @param labelId 主键
     * @return 是否成功
     */
    @Override
    public boolean deleteById(Integer labelId) {
        return this.storeLabelsDao.deleteById(labelId) > 0;
    }
}
